<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="../lib/vue.js"></script>
  </head>

  <body>
    <div id="app">
      <cpn :cnum1="num1" :cnum2="num2" @num1changed="num1changed"  @num2changed="num2changed"></cpn>
     
    </div>
    <template id="cpn">
      <div>
        <h2>cnum1:{{cnum1}}</h2>
        <h2>datanum1:{{datanum1}}</h2>
        <!--<div><input type="text" v-model="datanum1"></input></div>-->
        <div><input type="text" v-bind:value="datanum1" @input.number="num1input"></input></div>
        <hr/>
        <h2>cnum2:{{cnum2}}</h2>
        <h2>datanum2:{{datanum2}}</h2>
         <!--<div><input type="text" v-model="datanum2"></input></div>-->
         <div><input type="text" v-bind:value="datanum2" @input="num2input"></input></div>
      </div>
    </template>
    <script>
      var vm = new Vue({
        el: '#app',
        data: {
          num1: 0,
          num2: 1
        },
        components: {
          cpn: {
            template: '#cpn',
            props: { //将父组件的值传递给子组件
              cnum1: {
                type: Number,
                default: 2
              },
              cnum2: {
                type: Number,
                default: 2
              }
            },
            data(){
              return{
                datanum1:this.cnum1, //datanum1初始化为cnum1
                datanum2:this.cnum2
              }
            },
            methods:{
              num1input(event){
               
                this.datanum1=event.target.value

                this.$emit('num1changed',  this.datanum1) // 将datanum1的值绑定到num1changed事件传递给父组件

              },
              num2input(event){
                this.datanum2=event.target.value
                this.$emit('num2changed',  this.datanum2)
              }
            }
          }
        },
        methods: {
          num1changed(value){
            console.log(typeof value)
            // 将input中的value赋值给的number1
            this.num1=parseInt(value) //string 转为int
          },
          num2changed(value){
            console.log(typeof value)
             // 将input中的value赋值给的number2
            this.num2=parseFloat(value)
          }
        }
      })
    </script>
  </body>
</html>
